const { parse: vueParse, transform: vueTransform } = require('@vue/compiler-dom');
const MagicString = require('magic-string');

const KEY_DATA = 'data-v-inspector';
const EXCLUDE_TAG = ['template', 'script', 'style']
module.exports = function (source) {
    try {
        const s = new MagicString(source);
        const id = this.resourcePath;
        const relativePath = id;
        const ast = vueParse(source, { comments: true });
        vueTransform(ast, {
            nodeTransforms: [
                (node) => {
                    if (node.type === 1) {
                        // if (node.tagType === 0 && !EXCLUDE_TAG.includes(node.tag)) {
                        //     if (node.loc.source.includes(KEY_DATA))
                        //         return
                            const insertPosition = node.props.length ? Math.max(...node.props.map(i => i.loc.end.offset)) : node.loc.start.offset + node.tag.length + 1;
                            const { line, column } = node.loc.start;
                            const content = ` ${KEY_DATA}="${relativePath}:${line}:${column}"`;
                            s.prependLeft(insertPosition, content);
                        // }
                    }
                },
            ],
        });
        return s.toString();
    } catch (err) {
        console.log(err)
    }

};
